package com.et114.core.utility;



import java.util.*;

//import com.runqian.base4.util.StringUtils;


public class Section { //extends Vector{ //���ֽӿڲ��������£��ĳɼ̳�
  static public final short POS_UP = 1;
  static public final short POS_DOWN = 2;
  static public final short POS_TOP = 3;
  static public final short POS_BOTTOM = 4;

  public Vector sections = new Vector();
  private boolean allowSame = false;

  public Section() {
    this( false );
  }

  public Section( boolean allowSameSection) {
    this.allowSame = allowSameSection;
  }

  public Section(Vector items) {
    this( items, false );
  }

  public Section(Vector items, boolean allowSameSection) {
    this( allowSameSection);
    if (items == null) {
      return;
    }
    sections = (Vector) items.clone();
  }

  public Section(Set items) {
    this( items, false );
  }

  public Section(Set items, boolean allowSameSection) {
    this( allowSameSection );
    if (items == null) {
      return;
    }
    Iterator all = items.iterator();
    while (all.hasNext()) {
      sections.add(all.next());
    }
  }

  public Section(Object[] items) {
    this( items, false );
  }

  public Section(Object[] items, boolean allowSameSection) {
    this( allowSameSection );
    if (items == null) {
      return;
    }
    for (int i = 0; i < items.length; i++) {
      sections.add(items[i]);
    }
  }

  public Section(String items) {
    this( items, false );
  }

  public Section(String items, boolean allowSameSection) {
    this( allowSameSection );
    if (items == null) {
      return;
    }
    unionSection(items);
  }

  public Section(String items, char delim) {
    this( items, delim, false );
  }

  public Section(String items, char delim, boolean allowSameSection) {
    this( items, delim, allowSameSection, true);
  }

  public Section(String items, char delim, boolean allowSameSection, boolean removeEscape) {
    this( allowSameSection );
    if (items == null) {
      return;
    }
    unionSection(items, delim,removeEscape);
  }

  /**
   * �ϲ��ֽڴ�items���÷ֽ�ĩβ�����˵��Ѿ����ڵ�
   * @param items ��','�ֿ����ַ����
   */
  public void unionSection(String items) {
    unionSection( items, ',' );
  }

  public void unionSection(String items, char delim) {
    unionSection( items,delim,true);
  }

  public void unionSection(String items, char delim,boolean removeEscape) {
    if (items == null) {
      return;
    }
    ArgumentTokenizer st = new ArgumentTokenizer(items, delim);
    String s;
    while (st.hasMoreTokens()) {
      s = st.nextToken();
      if( removeEscape ){
	s = Escape.removeEscAndQuote(s);
      }
      addSection( s );
    }
  }

  
  /**
   * 是否有效的可见字符串
   * @param str Object，要判断的字符串对象
   * @return boolean，有效返回true，否则返回false
   */
  public static boolean isValidString(Object str) {
    if (! (str instanceof String)) {
      return false;
    }
    return!StringUtils.isSpaceString( (String) str);
  }
  
  /**
   * ��ӷֽڴ�item���÷ֽ�ĩβ
   * @paramb item �ֽڴ�
   */
  public void addSection(String item) {
    if (!isValidString(item)) {
      return;
    }
    if( allowSame || !sections.contains(item)){
      sections.add( item );
    }
  }

  public void insertSection(String item,int beforeIndex) {
    if (!isValidString(item)) {
      return;
    }
    if( allowSame || !sections.contains(item)){
      sections.insertElementAt( item, beforeIndex );
    }
  }

  /**
   * ɾ��ֽڴ�items
   * @param items ��','�ֿ����ַ����
   */
  public void removeSection(String items) {
    if (items == null) {
      return;
    }
    ArgumentTokenizer st = new ArgumentTokenizer(items, ',');

    while (st.hasMoreTokens()) {
      sections.remove(st.nextToken());
    }
  }

  /**
   * �ϲ��ֽڴ�items���÷ֽ�ĩβ
   * @param items �ֽڶ���
   */
  public void unionSection(Section items) {
    unionSection(items.toString());
  }

  /**
   * ���ظ÷ֽڴ���string����
   * @return ��','�ֿ����ַ�;ע�⣺�÷ֽڴ�Ҫ����Section��֣���ΪΪ�˱�֤�ڵ��ַ��
   * �����ԣ��ڽڵ��ַ�ǰ����������ַָ��
   */
  public String toSectionString() {
    StringBuffer buf = new StringBuffer(1024);
    for (int i = 0; i < sections.size(); i++) {
      buf.append(',');
      buf.append(Escape.addEscAndQuote( (String) sections.get(i)));
    }
    if (buf.length() == 0) {
      return "";
    }
    else {
      return buf.substring(1);
    }
  }

  public String toString() {
    return toString(',');
  }

  /**
   *
   * @param delim char
   * @return String ��charΪ�ָ��ŵ��ַ�
   */
  public String toString(char delim) {
    StringBuffer buf = new StringBuffer(1024);
    for (int i = 0; i < sections.size(); i++) {
      buf.append(delim);
      buf.append(sections.get(i));
    }
    if (buf.length() == 0) {
      return "";
    }
    else {
      return buf.substring(1);
    }
  }

  /**
   *���ؽڵ��ַ�ֵ
   * @param i �ڵ������
   * @return �ڵ��ַ�ֵ
   */
  public String getSection(int i) {
    if (sections.size() == 0) {
      return "";
    }
    if (i < 0 || i >= sections.size()) {
      return "";
    }
    return sections.get(i).toString();
  }

  public String get(int i) {
    return getSection(i);
  }

  /**
   * ���յ�ǰ�ڵ�˳�򣬲���section����ţ��˴���section����Ϊ����
   * �磺 ��ǰ��="a,b,c"  section = "a" return int[]={2,0}
   * @param sections Ҫ���ҵ��б�
   * @return ��������
   */
  public int indexOf(String section) {
    return sections.indexOf(section);
  }


  /**
   * �ӵ�ǰsection��ק����indexes��ָ��λ�õĽڹ���һ���µ�Section
   * @param indexes �ӽڵ���������
   * @return �µ��ӽ�
   */
  public Section extractSubSection(int[] indexes) {
    Section sRtn = new Section();
    for (int i = 0; i < indexes.length; i++) {
      if (indexes[i] < sections.size()) {
	sRtn.unionSection( (String) sections.get(indexes[i]));
      }
    }
    return sRtn;
  }

  /**
   * ��ڵ�ֵ����)��......
   * @param index
   * @param newItem
   */
  public void replaceSection(int index, String newItem) {
  }

  /**
   * �ƶ��ڵ����λ�ã���)��......
   * @param index
   * @param posCMD
   * @return
   */
  public int rePositionSection(int index, short posCMD) {
    return 1;
  }

  /**
   *�жϽ����Ƿ���ĳ��section
   * @param item �ڵ����
   * @return ����ýڷ���true,����false
   */
  public boolean containsSection(String item) {
    return sections.contains(item);
  }

  /**
   * ����ö����еķֽڸ���
   * @return �ڵ���Ŀ
   */
  public int countSection() {
    return sections.size();
  }

  /**
   * ����ö����еķֽڸ���
   * @return �ڵ���Ŀ
   */
  public int size() {
    return sections.size();
  }

  /**
   * ��յ�ǰ�Ľ�
   * @return 1
   */
  public int clear() {
    sections.clear();
    return 1;
  }

  public Vector toVector() {
    return sections;
  }

  public String[] toStringArray() {
    Object[] oa = sections.toArray();
    String[] sa = new String[oa.length];
    for(int i=0; i<oa.length; i++ ){
      sa[i] = ((String)oa[i]).trim();
    }
    return sa;
  }

}
